package nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by IntelliJ
 * Author:熊磊
 */
public class MyTest {
    public static void main(String[] args) throws IOException {
        FileInputStream fi = new FileInputStream(new File("1.txt"));
        FileOutputStream fs = new FileOutputStream(new File("2.txt"));
        FileChannel fiChannel = fi.getChannel();
        FileChannel fsChannel = fs.getChannel();
        ByteBuffer buf = ByteBuffer.allocate(128);
        int len = 0 ;
        while ((len= fiChannel.read(buf))!=-1){
            System.out.println("len的读取长度为: "+len);
            buf.flip();
            fsChannel.write(buf);
            buf.clear(); //不加clear 会导致 position = limit 导致读取得到的长度为0,且数据会重复 写入
        }

        fs.close();
        fi.close();

    }
}
